home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 440_01 / examples / ex_2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-16  |  9.3 KB  |  182 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <graphics.h>
  4. #include "colors.h"
  5. #include "!bestlib.h"
  6.  
  7.    /*** NOTE  the names and structure of the routines used in these examples
  8.               have changed in The Best Library 2.00; the updated examples will
  9.               be released soon                                            ***/
  10.  
  11. #define FILE 1
  12. #define SORT 2
  13. #define PRINT 3
  14. #define SCREEN 4
  15. #define DOS 5
  16.  
  17. #define NEW 1
  18. #define LOAD 2
  19. #define SAVE 3
  20. #define FONTSIZE 1
  21. #define BLOCK 1
  22. #define ENTIREDOCUMENT 2
  23. #define EXTERNALFILE 3
  24. #define EXITTO 1
  25. #define SHELLTO 2
  26.  
  27. #define BGCOLOR DARKGRAY               /* background color                  */
  28. #define TEXTCOLOR WHITE                /* color of text messages            */
  29. #define ABORTCOLOR RED                 /* color of abort messages           */
  30. #define ERASECOMMANDTEXT boxfill(0, txty, MAXX, txtht, BGCOLOR, COPY_IMAGE);
  31.  
  32.    /*** NOTE  even though this program only uses the "keyp" structure, the
  33.               other three are necessary for the assembler routines in the
  34.               !BESTLIB.LIB library to function properly.  C would also
  35.               produce a "linker warning" if you have enabled that warning ***/
  36. filldata fidata;                       /* create a "filldata" structure     */
  37. printdata prdata;                      /* create a "printdata" structure    */
  38. mousedata msdata;                      /* create a "mousedata" structure    */
  39. asciiscan keyp;                        /* create an "asciiscan" structure   */
  40.  
  41. char *menu1ptr[] = { "FILE", "SORT", "PRINT", "SCREEN", "DOS" };   /* menus */
  42. char *menu1text[] = {                                           /* submenus */
  43.    (char *)0x001D,
  44.    "NEW", "LOAD", "SAVE",
  45.    "BLOCK", "ENTIRE DOCUMENT", "EXTERNAL FILE",
  46.    "FONT SIZE",
  47.    "EXIT TO", "SHELL TO"
  48. };
  49. int menu1data[][3] = { { 3, 1, 0x0007 },
  50.                        { 0, 0, 0x0000 },
  51.                        { 3, 4, 0x0004 },
  52.                        { 1, 7, 0x0001 },
  53.                        { 2, 8, 0x0003 }
  54. };
  55.  
  56. void main(void)
  57. {
  58.    int gr_err, gdrv = VGA, gmod = VGAHI, fontsize = 5,
  59.        oldmode, mouse = FALSE, command = FALSE, submenu,
  60.        txtht = 15, txty = 465, menu, menux = MAXX / 3, menuy = 220;
  61.    char *errmsg;
  62.  
  63.    oldmode = readvideomode();          /* save the current video mode       */
  64.    if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
  65.                                        /* if the old mode was a text mode.. */
  66.       textmem(3);                      /* store text video memory           */
  67.       cursor(3, 1);                    /* hide and store cursor position    */
  68.    }
  69.    mouse = initms_gr();             /* initialize mouse driver if installed */
  70.    initgraph(&gdrv, &gmod, "");        /* initialize graphics mode          */
  71.    if ((gr_err = graphresult()) != grOk) {
  72.       /* if an error occurred in changing to graphics mode, print the error */
  73.       printf("Graphics error: %s\n", grapherrormsg(gr_err));
  74.       exit(1);                        /* exit to DOS returning errorlevel 1 */
  75.    }
  76.    settextjustify(LEFT_TEXT, TOP_TEXT);
  77.    settextstyle(SMALL_FONT, HORIZ_DIR, fontsize);  /* setup font size/style */
  78.    floodall(BGCOLOR);                  /* create the screen background      */
  79.    do {
  80.       command = popupmenu(menux, menuy, 1, 5,
  81.                           menu1ptr, menu1data, menu1text, mouse);
  82.       if (command == -1) break;       /* if unable to allocate memory, exit */
  83.       submenu = command & 0x000F, menu = command >> 4;
  84.       setcolor(TEXTCOLOR);             /* set the appropriate text color    */
  85.       switch(menu) {
  86.       case FILE:                       /* a selection from the file menu    */
  87.          switch(submenu) {
  88.          case NEW:                     /* the new submenu was selected      */
  89.             menu1text[0] = (char *)0x001D;  /* restore menu restrictions    */
  90.             menu1data[2][2] = 0x0004;       /* disable submenu restrictions */
  91.             ERASECOMMANDTEXT;               /* erase any previous messages  */
  92.             outtextxy(0, txty, "The NEW submenu was selected -- some options \
  93. disabled");
  94.             break;
  95.          case LOAD:                    /* the load submenu was selected     */
  96.             menu1text[0] = (char *)0x001F;  /* enable menu restrictions     */
  97.             menu1data[2][2] = 0x0007;       /* enable submenu restrictions  */
  98.             ERASECOMMANDTEXT;               /* erase any previous messages  */
  99.             outtextxy(0, txty, "The LOAD submenu was selected -- some options\
  100.  enabled");
  101.             break;
  102.          case SAVE: ;                  /* the save submenu was selected     */
  103.             ERASECOMMANDTEXT;          /* erase any previous messages       */
  104.             outtextxy(0, txty, "The SAVE submenu was selected");
  105.             break;
  106.          }
  107.          break;                        /* exit from switch statement        */
  108.       case SORT:                       /* the sort menu was selected        */
  109.          ERASECOMMANDTEXT;             /* erase any previous messages       */
  110.          outtextxy(0, txty, "The SORT menu was selected");
  111.          break;                        /* exit from switch statement        */
  112.       case PRINT:                      /* a selection from the print menu   */
  113.          switch(submenu) {
  114.          case BLOCK:                   /* the block submenu was selected    */
  115.             ERASECOMMANDTEXT;          /* erase any previous messages       */
  116.             outtextxy(0, txty, "The BLOCK submenu was selected");
  117.             break;
  118.          case ENTIREDOCUMENT:          /* the entiredocument was selected   */
  119.             ERASECOMMANDTEXT;          /* erase any previous messages       */
  120.             outtextxy(0, txty, "The ENTIRE DOCUMENT submenu was selected");
  121.             break;
  122.          case EXTERNALFILE: ;          /* the externalfile was selected     */
  123.             ERASECOMMANDTEXT;          /* erase any previous messages       */
  124.             outtextxy(0, txty, "The EXTERNAL FILE submenu was selected");
  125.             break;
  126.          }
  127.          break;                        /* exit from switch statement        */
  128.       case SCREEN:                     /* a selection from the fonts menu   */
  129.          switch(submenu) {
  130.          case FONTSIZE:         /* the select new font submenu was selected */
  131.             textm(0);                  /* make sure we are in text mode     */
  132.             clear(LIGHTGRAY, BLACK);   /* clear the screen and set colors   */
  133.             printsatxy("What font size would you like to use (1-7)?", 10, 12);
  134.             readnumber(7, 1, 54, 12, &fontsize);  /* ask user for font size */
  135.             initgraph(&gdrv, &gmod, "");     /* re-initialize graphics mode */
  136.             settextstyle(SMALL_FONT, HORIZ_DIR, fontsize);     /* font size */
  137.             txtht = textheight("A") + 3;     /* initialize text height      */
  138.             txty = MAXY - txtht;             /* initialize text y-coord     */
  139.             menux = MAXX - (textwidth(menu1ptr[3])+17 +
  140.                     textwidth(menu1text[4])+33 + textwidth(menu1text[5])+33 +
  141.                     textwidth(menu1text[6])+33);  /* calculate text x-coord */
  142.             if (menux > MAXX / 3) menux = MAXX / 3;
  143.             floodall(BGCOLOR);         /* create the screen background      */
  144.          }
  145.          break;                        /* exit from switch statement        */
  146.       case DOS:                        /* a selection from the DOS menu     */
  147.          switch(submenu) {
  148.          case EXITTO: break;           /* the exit to submenu was selected  */
  149.          case SHELLTO:                 /* the shell to submenu was selected */
  150.             if ((errmsg = dosshell()) != NULL) {
  151.                ERASECOMMANDTEXT;       /* erase any previous messages       */
  152.                while (textwidth(errmsg) > MAXX)  /* entire message must fit */
  153.                   settextstyle(SMALL_FONT, HORIZ_DIR, --fontsize);
  154.                outtextxy(0, txty, errmsg);  /* print the returned error msg */
  155.             }
  156.             else {                     /* else if no error occurred..       */
  157.                highstandardvga();      /* get back into 640x480x16 mode VGA */
  158.                settextjustify(LEFT_TEXT, TOP_TEXT);
  159.                settextstyle(SMALL_FONT, HORIZ_DIR, fontsize);
  160.                floodall(BGCOLOR);      /* restore the screen background     */
  161.                outtextxy(0, txty, "Welcome back!");
  162.             }
  163.             break;
  164.          }
  165.          break;
  166.       case FALSE:                      /* no selection was registered       */
  167.          setcolor(ABORTCOLOR);         /* set the appropriate abort color   */
  168.          ERASECOMMANDTEXT;             /* erase any previous messages       */
  169.          outtextxy(0, txty, "Operation aborted");
  170.          setcolor(TEXTCOLOR);          /* restore text message color        */
  171.          break;                        /* exit from switch statement        */
  172.       }
  173.    } while (menu != DOS || submenu != EXITTO);  /* loop until exit selected */
  174.  
  175.    changevideomode(oldmode);           /* restore the original video mode   */
  176.    if (oldmode == 3 || oldmode == 7 || oldmode == 21) {
  177.                                        /* if the old mode was a text mode.. */
  178.       textmem(1);                      /* restore text video memory         */
  179.       cursor(1, 1);                    /* show and restore cursor position  */
  180.    }
  181. }
  182.